<template>
  <div class="streetSweeping">
    <!-- tab切换标签 -->
    <div class='tabs_area'>
      <span :class='showDay?"active":"left"' @click='changeSpecial(1)'>
        <div>成都特价</div>
        <div v-show="this.showDay" class='line'></div>
      </span>
      <span :class='showRoutine?"active":"right"' @click='changeSpecial(2)'>
        <div>普通特价</div>
        <div v-show="this.showRoutine" class='line'></div>
      </span>
    </div>
    <TopBar :dataSourse='promoteSalesBanner' />
    <!-- //活动特价 -->
    <div class='routineSpecials' v-if="this.showDay">
      <div class='search_area'>
        <div class='divtip'>
          <input type="text" v-model="temp.keyWord" placeholder="输入商品名、商品首字母、厂家">
          <div class='div' @click='search(temp.keyWord)'>搜索</div>
        </div>
      </div>
      <Scroller ref="scroller" @load='promotionActivity()'>
        <GoodsList couponType='coupon' @showCards='showCards' :dataSoures='routineGoodsList' />
      </Scroller>
    </div>
    <!-- //常规特价 -->
    <div class='routineSpecials' v-if="this.showRoutine">
      <div class='search_area'>
        <div class='divtip'>
          <input type="text" v-model="temp.keyWord" placeholder="输入商品名、商品首字母、厂家">
          <div class='div' @click='search(temp.keyWord)'>搜索</div>
        </div>
      </div>
      <!-- //商品列表 -->
      <div class='routineSpecials_content'>
        <Scroller ref="scroller" @load='promotionActivity()'>
          <GoodsList couponType='coupon' @showCards='showCards' :dataSoures='routineGoodsList' />
        </Scroller>
      </div>
    </div>
    <!-- //弹窗详情 -->
    <DialogInfo :everyDayInfo='everyDayInfo' :show='show' @close='close' />
    <div class='upgrade' @click='goTop'>
      <img src="https://image.hezongyy.com/pcimage/119h5/goup.png" alt="">
    </div>
  </div>
</template>
<script>
import { getRequest } from '@/api/indexGet'
import { postRequest } from '@/api/index'
import url from '@/axios/apiUrl'
import Xpng from '@/assets/image/X.png'
import TopBar from './topBar.vue'
import DialogInfo from '@/components/DialogInfo'
import { Scroller, GoodsList } from '@/components/Scroller'
export default {
  components: {
    TopBar,
    DialogInfo,
    GoodsList,
    Scroller,
  },
  data() {
    return {
      everyDayKeyWord: '', //每日特价搜索词
      weekKeyWord: '', //预告预告搜索词
      Xpng, //图片关闭按钮
      showDay: true, //活动特价是否开启
      showRoutine: false, //常规特价是否开启
      routineGoodsList: [], //常规特价商品列表
      temp: {
        drugType: 0,
        isActivityPromotion: 0,
        isChoose: 0,
        isGift: 0,
        pageNumber: 1,
        pageSize: 40,
        keyWord: '', //常规特价搜索词
      },
      show: false, //弹窗开关
      everyDayInfo: {}, ///弹窗信息详情
      promoteSalesBanner: [], //每日特价 banner
    }
  },
  async mounted() {
    //顶部banner轮播图
    this.adPost()
    this.promotionActivity()
  },
  methods: {
    //回到顶部
    goTop() {
      let node = document.querySelector('.scroller')
      node.scrollTop = 0
    },
    //顶部banner
    async adPost() {
      let res = await postRequest(url.adPost, {
        pageName: 'promoteSales',
        clientType: 1,
      })
      if (res.data.code == '000000') {
        this.promoteSalesBanner = res.data.content.promoteSalesBanner.items
      }
    },
    //加入购物车
    async submit(item) {
      if (item.quantity != 0) {
        if (item.restriction == 0) {
          let res = await postRequest(url.addShopCarNew, {
            goodsId: item.id,
            itemType: item.itemType,
            goodsQuantity: item.initializeMinimum,
          })
          if (res.data.code == '000000') {
            this.$toast('加入购物车成功~')
            this.show = false
          }
          if (res.data.message == '系统异常') {
            this.$toast('网络异常，请稍后再试')
          } else if (res.data.code != '000000') {
            this.$toast(res.data.message)
          }
        } else {
          if (item.initializeMinimum > item.maximum) {
            this.$toast('已超过最大购买量，不能加入购物车')
          } else {
            let res = await postRequest(url.addShopCarNew, {
              goodsId: item.id,
              itemType: item.itemType,
              goodsQuantity: item.initializeMinimum,
            })
            if (res.data.code == '000000') {
              this.$toast('加入购物车成功~')
              this.show = false
            }
            if (res.data.message == '系统异常') {
              this.$toast('网络异常，请稍后再试')
            } else if (res.data.code != '000000') {
              this.$toast(res.data.message)
            }
          }
        }
      } else {
        this.$toast('该商品已售罄')
      }
    },
    //关闭弹窗
    close() {
      this.show = false
    },
    //加购弹窗
    showCards(item) {
      this.show = true
      this.everyDayInfo = item
    },
    //常规特价搜索
    async search(value, name) {
      this.temp.keyWord = value
      this.temp.pageNumber = 1
      await this.promotionActivity()
      this.goTop()
    },
    //常规特价
    async promotionActivity() {
        this.activeLoadingShow = true
      if (this.showDay) {
        this.temp.isActivityPromotion = 0
      } else {
        this.temp.isActivityPromotion = 1
      }
      let res = await postRequest(url.promotionActivity, this.temp)
      if (res.data.code == '000000') {
        this.activeLoadingShow = false
        if (this.temp.pageNumber == 1) {
          this.routineGoodsList = []
        }
        this.temp.pageNumber++
        this.routineGoodsList = this.routineGoodsList.concat(
          res.data.content.list
        )
        if (
          res.data.content.list.length <40 ||
          res.data.content.list.length == null
        ) {
          // 数据全部加载完成
          this.$refs['scroller'].loadOver()
        } else {
          this.$refs['scroller'].canLoad()
        }
        if (this.routineGoodsList) {
          this.routineGoodsList.map((obj) => {
            this.$set(obj, 'initializeMinimum', 1)
            this.$set(obj, 'maximum', obj.maxBuyNumber)
            this.goodsAdd.pageInit(obj, this)
          })
        }
      } else {
        this.activeLoadingShow = false
        this.$toast(res.data.message)
      }
    },

    //选择tab切换活动 常规 预告预告
    async changeSpecial(index) {
      // 1每日特价2常规特价
      this.routineGoodsList = []
      this.temp.pageNumber = 1
      this.temp.keyWord = ''
      this.everyDayKeyWord = ''
      if (index == 1) {
        this.showDay = true
        this.showRoutine = false
      } else if (index == 2) {
        this.showDay = false
        this.showRoutine = true
      }
      this.$nextTick()
      this.promotionActivity()
      this.goTop()
    },
  },
}
</script>
<style lang="less" src="./index.less" scoped></style>
